home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap18 / Emf1 / Emf1.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.0 KB  |  99 lines

  1. /*-------------------------------------
  2.    EMF1.C -- Enhanced Metafile Demo #1
  3.              (c) Charles Petzold, 1998
  4.   -------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     LPSTR lpszCmdLine, int nCmdShow)
  12. {
  13.      static TCHAR szAppName[] = TEXT ("EMF1") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.      
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.  
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Enhanced Metafile Demo #1"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, nCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  54. {
  55.      static HENHMETAFILE hemf ;
  56.      HDC                 hdc, hdcEMF ;
  57.      PAINTSTRUCT         ps ;
  58.      RECT                rect ;
  59.      
  60.      switch (message)
  61.      {
  62.      case WM_CREATE:
  63.           hdcEMF = CreateEnhMetaFile (NULL, NULL, NULL, NULL) ;
  64.  
  65.           Rectangle (hdcEMF, 100, 100, 200, 200) ;
  66.           
  67.           MoveToEx  (hdcEMF, 100, 100, NULL) ;
  68.           LineTo    (hdcEMF, 200, 200) ;
  69.           
  70.           MoveToEx  (hdcEMF, 200, 100, NULL) ;
  71.           LineTo    (hdcEMF, 100, 200) ;
  72.  
  73.           hemf = CloseEnhMetaFile (hdcEMF) ;
  74.           return 0 ;
  75.           
  76.      case WM_PAINT:
  77.           hdc = BeginPaint (hwnd, &ps) ;
  78.           
  79.           GetClientRect (hwnd, &rect) ;
  80.           
  81.           rect.left   =     rect.right  / 4 ;
  82.           rect.right  = 3 * rect.right  / 4 ;
  83.           rect.top    =     rect.bottom / 4 ;
  84.           rect.bottom = 3 * rect.bottom / 4 ;
  85.           
  86.           PlayEnhMetaFile (hdc, hemf, &rect) ;
  87.           
  88.           EndPaint (hwnd, &ps) ;
  89.           return 0 ;
  90.           
  91.      case WM_DESTROY:
  92.           DeleteEnhMetaFile (hemf) ;
  93.           
  94.           PostQuitMessage (0) ;
  95.           return 0 ;
  96.      }
  97.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  98. }
  99.